import VueRouter from "vue-router";
import Vue from "vue";
import Layout from "@/views/layout";

Vue.use(VueRouter);

//导入动态路由配置
import BaseRoute from "./modules/base";
import CompanysRoute from "./modules/companys";
import QuestionsRoute from "./modules/questions";
import SubjectsRoute from "./modules/subjects";
import ArticlesRoute from "./modules/articles";

//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

//静态路由
const constRoutes = [
  {
    path: "/",
    component: Layout,
    meta: { title: "黑马面面" },
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard"),
        meta: { title: "数据概览" },
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login"),
    meta: { title: "登录" },
  },
  {
    path: "/404",
    component: () => import("@/views/notfound"),
  },
  {
    path: "/*",
    redirect: "/404",
  },
];

//动态路由(业务路由)
const asyncRoutes = [BaseRoute, CompanysRoute, QuestionsRoute, SubjectsRoute, ArticlesRoute];

const router = new VueRouter({
  routes: [...asyncRoutes, ...constRoutes],
});

export default router;
